<template>
  <div class="box">
    <h1>公众号功能页面</h1>
    <a :href="`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${confg.appid}&redirect_uri=${confg.redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`">
      网页静默授权
    </a>
    <br>
    <a :href="`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${confg.appid}&redirect_uri=${confg.redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`">
      网页授权登录
    </a>
    <br>
    {{ userinfo }}
    <button @click="getUserInfo">
      公众号code换取用户信息
    </button>
    <button @click="getOpenId">
      静默登录获取openId
    </button>
    <br>
    openid
    <input v-model="params.openid">
    phoneNo
    <input v-model="params.phoneNo">
    price
    <input v-model="params.price">
    number
    <input v-model="params.number">
    mallId
    <input v-model="params.mallId">
    <button @click="settlement">
      统一下单
    </button>
    <br>
    payParams: {{ payParams }}
    <br>
    <button @click="wxPay">
      调用支付
    </button>
  </div>
</template>

<script>
import confg from "./confg"
import { wxLogin, settlement, wxLoginBak } from "./WxApi"
export default {
  data() {
    return {
      confg,
      code: undefined,
      userinfo: {},
      params: { openid: '' },
      payParams: {}
    }
  },
  mounted() {
    this.code = this.$route.query.code;
    console.log(this.code)
  },
  methods: {
    getOpenId() {
      wxLoginBak({ code: this.code }).then(res => {
        // window.$Cookie.set('mbl_token', res.token)
        this.params.openid = res.openId
        console.log(this.params)
      })
    },
    wxPay() {
      // eslint-disable-next-line no-undef
      WeixinJSBridge.invoke('getBrandWCPayRequest', this.payParams, (res) => {
        if (res.err_msg == "get_brand_wcpay_request:ok") {
          console.log(res)
        }
      })
    },
    settlement() {
      // , ...{ token: window.$Cookie.get('mbl_token')
      settlement({ ...this.params }).then(res => {
        // res.package = res.packageValue
        // delete res.packageValue
        this.payParams = JSON.parse(res)
        console.log(this.payParams)
      })
    },
    getUserInfo() {
      if (this.code) {
        wxLogin({ code: this.code }).then(res => {
          this.userinfo = res.user
          window.$Cookie.set('ntd_token', res.token)
          this.params.openid = res.user.openid
          console.log(res)
        })
      } else {
        console.log('清先获取code信息！')
      }
    }
  }
}
</script>

<style scoped>
.box {
  line-height: 50px;
}
</style>
